﻿package{
    /**
     *@author:Childhood
     *@website:flashas3.diandian.com
     */
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.ui.Mouse;
    public class CoreDemo2 extends Sprite{
        private var canvas:Canvas;
        private var maskMc:Sprite;
        public function CoreDemo2():void{
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
        }

        private function onAddedToStage(event:Event):void{
            removeEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
            init();
        }

        private function init():void{
            canvas = new Canvas();
            canvas.x = 80;
            canvas.y = 60;
            addChild(canvas);
            trace(canvas.width);
            trace(canvas.height);

            maskMc = new Sprite();
            maskMc.graphics.beginFill(0x000000, 0.8);
            maskMc.graphics.drawRect(80, 60, Canvas.CANVAS_WIDTH, Canvas.CANVAS_HEIGHT);
            maskMc.graphics.endFill();
            canvas.mask = maskMc;
            //add button event listeners
            freeHandBtn.addEventListener(MouseEvent.CLICK, onChooseFreeHand);
            lineBtn.addEventListener(MouseEvent.CLICK, onChooseLine);
            eraseBtn.addEventListener(MouseEvent.CLICK, onErase);
            undoBtn.addEventListener(MouseEvent.CLICK, onUndo);
            redoBtn.addEventListener(MouseEvent.CLICK, onRedo);
            clearBtn.addEventListener(MouseEvent.CLICK, onClearCanvas);

            freeHandBtn.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
            lineBtn.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver); 
            eraseBtn.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
            undoBtn.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
            redoBtn.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
            clearBtn.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
        }


/*----------------------------button event handlers--------------------------------*/
        private function onChooseFreeHand(e:MouseEvent):void{
            canvas.statusId = Canvas.FREE_HAND;
            canvas.cursor = "star";
        }

        private function onChooseLine(e:MouseEvent):void{
            canvas.statusId = Canvas.LINE;
            canvas.cursor = "star";
        } 

        private function onErase(e:MouseEvent):void{
            canvas.statusId = Canvas.ERASE;
            canvas.cursor = "eraser";
        }

        private function onUndo(e:MouseEvent):void{
            if(canvas.boxArr.length > 0){
                var graph:Sprite = canvas.boxArr.pop();
                canvas.removeChild(graph);
                canvas.redoList.push(graph);
                trace("undo success!");
            }
        }

        private function onRedo(e:MouseEvent):void{
            if(canvas.redoList.length > 0){
                var graph:Sprite = canvas.redoList.pop();
                canvas.addChild(graph);
                canvas.boxArr.push(graph);
                trace("redo success!");
            }
        }

        private function onClearCanvas(e:MouseEvent):void{
            while(canvas.boxArr.length > 0){
                var graph:Sprite = canvas.boxArr.pop();
                canvas.removeChild(graph);
            }
        }

        private function onMouseOver(e:MouseEvent):void{
            Mouse.show();
        }
    }
}
